package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;

import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.bean.DeviceAddedResponseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IDeviceAddedView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 已添加设备
 * @Date: 2023/3/18 16:15
 */
public class DeviceAddedPresenter {
    private final IDeviceAddedView deviceAddedView;

    public DeviceAddedPresenter(IDeviceAddedView deviceAddedView) {
        this.deviceAddedView = deviceAddedView;
    }

    public void getDeviceAdded() {
        deviceAddedView.showLoading();
        Call<BaseBean<DeviceAddedResponseBean>> call = RetrofitManager.getInstance().getService().getDeviceAdded("1", "50");
        call.enqueue(new Callback<BaseBean<DeviceAddedResponseBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<DeviceAddedResponseBean>> call, @NonNull Response<BaseBean<DeviceAddedResponseBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<DeviceAddedResponseBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        deviceAddedView.success(baseBean.data.list);
                    } else {
                        deviceAddedView.failure(baseBean.msg);
                    }
                } else {
                    deviceAddedView.failure("request failed");
                }
                deviceAddedView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<DeviceAddedResponseBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                deviceAddedView.failure("request failed");
                deviceAddedView.hideDialog();
            }
        });
    }
}
